#include "stm32f10x.h"                  // Device header
#include "string.h"
#include "mqtt.h"
#include "serial2.h"
void Timer_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	
	TIM_InternalClockConfig(TIM2);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=400-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler=7200-1;
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);//预防初始化后直接进入中断
	
	TIM_Cmd(TIM2,DISABLE);
}

void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
	{
		memcpy(&mqtt_RxInPtr[2],USART2_RxBuff,USART2_RxCounter);  
		mqtt_RxInPtr[0] = USART2_RxCounter/256;                   
		mqtt_RxInPtr[1] = USART2_RxCounter%256;                   
		mqtt_RxInPtr+=400;                                  
		if(mqtt_RxInPtr>mqtt_RxEndPtr)                       
			mqtt_RxInPtr = mqtt_RxBuf[0]; 
		USART2_RxCounter=0;  
		memset(USART2_RxBuff,0,1024);
		TIM_Cmd(TIM2,DISABLE);
		TIM_SetCounter(TIM2,0);
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
	}
}

